package com.taptech.doufu.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.annotation.SuppressLint;


public class TimeUtil {
	public static final long HOUR = 60 * 60 * 1000;
	public static final long DAY = 24 * HOUR;
    public static final  String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static String getShowTime() {

		Date date = new Date();

		long time = date.getTime();

		String dateline = time + "";
		TTLog.e("getShowTime", dateline);
		dateline = dateline.substring(0, 10);
		TTLog.e("getShowTime", dateline);
		return dateline;
	}

	public static int getDayOfWeek() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * return the y_m_d String 
	 * */
	public static String getToday() {
		Calendar calendar = Calendar.getInstance();

		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONDAY) + 1;
		int day = calendar.get(Calendar.DATE);

		return (year + "-" + month + "-" + day);
	}

	/**
	 * y-m-d 00:00
	 * 
	 * */
	public static String getDate() {
		Calendar calendar = Calendar.getInstance();

		int year = calendar.get(Calendar.YEAR);

		int month = calendar.get(Calendar.MONDAY) + 1;

		String monthStr = month < 10 ? "0" + month : "" + month;
		int day = calendar.get(Calendar.DATE);

		String dayStr = day < 10 ? "0" + day : "" + day;

		int hour = calendar.get(Calendar.HOUR_OF_DAY);

		String hourStr = hour < 10 ? "0" + hour : "" + hour;

		int min = calendar.get(Calendar.MINUTE);

		String minStr = min < 10 ? "0" + min : "" + min;

		int sec = calendar.get(Calendar.SECOND);

		String secStr = sec < 10 ? "0" + sec : "" + sec;

		return (year + "-" + monthStr + "-" + dayStr + " " + hourStr + ":" + minStr + ":" + secStr);

	}
	/**
	 * compare two timestamp , whether if in the same day
	 * 
	 * */
	public static boolean compareTime(String l) {

		SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyy年MM月dd");     
		Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间     
		String   nowTime   =   formatter.format(curDate);
		Date  lastDate=new Date(Long.parseLong(l)*1000);
		String   loastTime   =   formatter.format(lastDate);
		return nowTime.equals(loastTime);
	}
	
	/**
	 * y-m-d 00:00:00
	 * 
	 * */
	@SuppressLint("SimpleDateFormat")
	public static  String getNowData(){
		SimpleDateFormat    sDateFormat    =   new    SimpleDateFormat("yyyy-MM-dd    hh:mm:ss");       
		String    date    =    sDateFormat.format(new    java.util.Date()); 
		return date;
	}

	public static String getMD5Time() {

		TimeZone.setDefault(TimeZone.getTimeZone("GMT+0"));

		long millis = new Date().getTime();
		// current - current % DAY means the 0 time zone's time, 
		// - 8 hours , change to beijing time , beacause server is in beijing

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 

		return sdf.format(millis);
	}


	public static String getDate(long millis) {

		//Date date = new Date(millis);
		//Timestamp ts = new Timestamp(millis);



		TTLog.e("getDate", millis + "");

		/*int year = time.getYear();

		int month = time.getMonth();

		String monthStr = month < 10 ? "0" + month : "" + month;
		int day = time.getDay();

		String dayStr = day < 10 ? "0" + day : "" + day;

		int hour = time.getHours();

		String hourStr = hour < 10 ? "0" + hour : "" + hour;

		int min = time.getMinutes();

		String minStr = min < 10 ? "0" + min : "" + min;

		int sec = time.getSeconds();

		String secStr = sec < 10 ? "0" + sec : "" + sec;

		return (year + "-" + monthStr + "-" + dayStr + " " + hourStr + ":" + minStr + ":" + secStr);*/

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

		return sdf.format(millis);
	}

	/**
	 * compare two timestamp , whether if in the same day
	 * 
	 * @return , if > 0 , bigger, else if < 0 , litte, else equal
	 * */
	public static long compareTime(long l, long r) {
		
		SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyy年MM月dd日");     
		Date   curDate   =   new   Date(r);//获取当前时间     
		String   str   =   formatter.format(curDate);
		TTLog.e("compareTime", "==l:" + l/DAY + "->"+ "r:" + r/DAY+"=="+str);
		TTLog.e("compareTime", "l====="+l+"r===="+r);
		return (l / DAY - r / DAY);
	}


	/**
	 * @param lastTime, lastTime modified
	 * @param nowTime, current time
	 * 
	 * @param interval , min time (HOUR)
	 * */
	public static boolean isNeedUpdate(long lastTime, long nowTime, float interval) {

		if (nowTime - lastTime >= interval * HOUR) {
			return true;
		}

		return false;
	}

	public static String cutOutSecondsFromDate(String date) {

		if (date == null || date.length() < 4) {
			return date;
		}

		return date.substring(0, date.length() - 3);
	}
	@SuppressLint("SimpleDateFormat")
	public static String getNowTime(){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyy年MM月dd日");     
		Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间     
		String   str   =   formatter.format(curDate);
		return str; 
	}
	@SuppressLint("SimpleDateFormat")
	public static String IntChangeDate(String  cc_time){
		TTLog.s("TimeZone.getDefault().getRawOffset()=="+TimeZone.getDefault().getRawOffset());
		String re_StrTime ="";
		if(cc_time==null||"".equals(cc_time)){
			return re_StrTime;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;
		
	}
	//yyyy-mm-dd
	@SuppressLint("SimpleDateFormat")
	public static String IntChangeDateSecond(String  cc_time){
		TTLog.s("TimeZone.getDefault().getRawOffset()=="+TimeZone.getDefault().getRawOffset());
		String re_StrTime ="";
		if(cc_time==null||"".equals(cc_time)){
			return re_StrTime;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;
		
	}
	//yyyy-MM-dd
	@SuppressLint("SimpleDateFormat")
	public static String getNowTimeTo(){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat("yyyy-MM-dd");     
		Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间     
		String   str   =   formatter.format(curDate);
		return str; 
		
	}
	@SuppressLint("SimpleDateFormat")
	public static String getNowTimeTo(String format){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat(format);
		Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间
		String   str   =   formatter.format(curDate);
		return str;

	}

	@SuppressLint("SimpleDateFormat")
	public static String get24HourLaterTimeTo(String format){
		SimpleDateFormat   formatter   =   new   SimpleDateFormat(format);
		Date   curDate   =   new   Date(System.currentTimeMillis()+24*60*60*1000);//获取当前时间
		String   str   =   formatter.format(curDate);
		return str;

	}
	@SuppressLint("SimpleDateFormat")
	public static String chineselDate(String  cc_time,int mode){
		String re_StrTime ="";
		if(cc_time==null||"".equals(cc_time)){
			return re_StrTime;
		}	
		SimpleDateFormat sdf =null;
		switch (mode) {
		case 0:
			sdf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
			break;
		case 1:
			sdf = new SimpleDateFormat("yyyy年MM月dd日 ");
			break;
		case 2:
			sdf = new SimpleDateFormat("HH:mm");
			break;
		default:
			break;
		}
		sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		long lcc_time = Long.valueOf(cc_time);
		re_StrTime = sdf.format(new Date(lcc_time * 1000L));
		return re_StrTime;	
	}
	@SuppressLint("SimpleDateFormat")
	public static boolean compareDate(String cc_time){
		boolean isDateEffective=false;
		long seconds = Long.valueOf(cc_time);
		long space =  seconds-System.currentTimeMillis() / 1000 ;
		if(space>0){
			isDateEffective=true;	
		} 
		return isDateEffective;	
	}
	@SuppressLint("SimpleDateFormat")
	public static boolean compareDate24(String cc_time){
		boolean isDateEffective=false;
		long seconds = Long.valueOf(cc_time);
		long space = System.currentTimeMillis() / 1000- seconds ;
		if(space<24*60*60){
			isDateEffective=true;	
		} 
		return isDateEffective;	
	}

	public static boolean compareTimeSize(String time1,String time2) throws ParseException
	{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date a=sdf.parse(time1);
		Date b=sdf.parse(time2);
		//Date类的一个方法，如果a早于b返回true，否则返回false
		if(a.before(b))
			return true;
		else
			return false;

	}
}
